<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:marc="http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	exclude-result-prefixes="marc">
	<xsl:output method="xml" indent="yes" />

	<xsl:template match="record">
		<resource>
			<instance>
				<medium>Electronic</medium>
				<xsl:apply-templates select="controlfield"></xsl:apply-templates>
				<xsl:apply-templates select="datafield"></xsl:apply-templates>
			</instance>
		</resource>
	</xsl:template>

	<!-- Suppress all unmatched text/attributes -->
	<xsl:template match="text()|@*" />

	<xsl:template match="controlfield[@tag='001']">
		<identifier>
			URN:MetalibIRD:
			<xsl:value-of select="." />
		</identifier>
	</xsl:template>
	<xsl:template match="datafield[@tag='245']">
		<title>
			<xsl:call-template name="subfieldSelect">
				<xsl:with-param name="codes">
					abfghk
				</xsl:with-param>
			</xsl:call-template>
		</title>
	</xsl:template>
	<xsl:template match="datafield[@tag='110']">
		<publisher>
			<xsl:call-template name="subfieldSelect"></xsl:call-template>
		</publisher>
	</xsl:template>
	<xsl:template match="datafield[@tag='655']">
		<type>
			<xsl:call-template name="subfieldSelect"></xsl:call-template>
		</type>
		<format>
			<xsl:call-template name="subfieldSelect"></xsl:call-template>
		</format>
	</xsl:template>
	<xsl:template match="datafield[@tag='520']">
		<description>
			<xsl:value-of select="subfield[@code='a']" />
		</description>
	</xsl:template>
	<xsl:template match="datafield[@tag='500' or @tag='505']">
		<description>
			<xsl:value-of select="subfield[@code='a']" />
		</description>
	</xsl:template>
	<xsl:template match="datafield[@tag='531']">
		<description>
			<xsl:value-of select="subfield[@code='a']" />
		</description>
	</xsl:template>
	<xsl:template match="datafield[@tag='540']">
		<rights>
			<xsl:value-of select="subfield[@code='a']" />
		</rights>
	</xsl:template>
	<xsl:template match="datafield[@tag='546']">
		<language>
			<xsl:value-of select="subfield[@code='a']" />
		</language>
	</xsl:template>
	<xsl:template match="datafield[@tag='594']">
		<status>
			<xsl:value-of select="subfield[@code='a']" />
		</status>
	</xsl:template>
	<xsl:template match="datafield[@tag='595']">
		<description>
			<xsl:value-of select="subfield[@code='a']" />
		</description>
	</xsl:template>
	<xsl:template match="datafield[@tag='653' or @tag='710']">
		<subject>
			<xsl:value-of select="subfield[@code='a']" />
		</subject>
	</xsl:template>

	<xsl:template match="datafield[@tag='856' and @ind1='4']">
		<xsl:choose>
			<xsl:when test="@ind2='1'">
				<url>
					<xsl:value-of select="subfield[@code='u']" />
				</url>
			</xsl:when>
			<xsl:when test="@ind2='2'">
				<url type="version">
					<xsl:value-of select="subfield[@code='u']" />
				</url>
			</xsl:when>
			<xsl:when test="@ind2='3'">
				<url type="related">
					<xsl:value-of select="subfield[@code='u']" />
				</url>
			</xsl:when>
			<xsl:when test="@ind2='9'">
				<url type="other">
					<xsl:value-of select="subfield[@code='u']" />
				</url>
			</xsl:when>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="datafield[@tag='246']">
		<alternateTitle>
			<xsl:call-template name="subfieldSelect"></xsl:call-template>
		</alternateTitle>
	</xsl:template>

	<xsl:template match="datafield[@tag='AF1']">
		<library>
			<xsl:value-of select="subfield[@code='a']" />
		</library>
	</xsl:template>

	<xsl:template name="subfieldSelect">
		<xsl:param name="codes">
			abcdefghijklmnopqrstuvwxyz
		</xsl:param>
		<xsl:param name="delimeter">
			<xsl:text> </xsl:text>
		</xsl:param>
		<xsl:variable name="str">
			<xsl:for-each select="subfield">
				<xsl:if test="contains($codes, @code)">
					<xsl:value-of select="text()" />
					<xsl:value-of select="$delimeter" />
				</xsl:if>
			</xsl:for-each>
		</xsl:variable>
		<xsl:value-of
			select="substring($str,1,string-length($str)-string-length($delimeter))" />
	</xsl:template>


</xsl:stylesheet>